-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
TextField.swift
executable file
·138 lines (114 loc) · 3.53 KB
/
TextField.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import UIKit
class TextField: UITextField {
var shouldAnimateStateChange: Bool = true
var shouldChangeColorWhenEditing: Bool = true
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
func setup() {
borderStyle = .none
layer.cornerRadius = 0
layer.masksToBounds = true
layer.borderWidth = 1
tintColor = .black
font = UIFont.serifFont(withSize: self.font?.pointSize ?? 26)
stateChangedAnimated(false)
setupEvents()
}
func setupEvents () {
addTarget(self, action: #selector(TextField.editingDidBegin(_:)), for: .editingDidBegin)
addTarget(self, action: #selector(TextField.editingDidFinish(_:)), for: .editingDidEnd)
}
func editingDidBegin (_ sender: AnyObject) {
stateChangedAnimated(shouldAnimateStateChange)
}
func editingDidFinish(_ sender: AnyObject) {
stateChangedAnimated(shouldAnimateStateChange)
}
func stateChangedAnimated(_ animated: Bool) {
let newBorderColor = borderColorForState().cgColor
if newBorderColor == layer.borderColor {
return
}
if animated {
let fade = CABasicAnimation()
if layer.borderColor == nil { layer.borderColor = UIColor.clear.cgColor }
fade.fromValue = self.layer.borderColor ?? UIColor.clear.cgColor
fade.toValue = newBorderColor
fade.duration = AnimationDuration.Short
layer.add(fade, forKey: "borderColor")
}
layer.borderColor = newBorderColor
}
func borderColorForState() -> UIColor {
if isEditing && shouldChangeColorWhenEditing {
return .artsyPurpleRegular()
} else {
return .artsyGrayMedium()
}
}
func setBorderColor(_ color: UIColor) {
self.layer.borderColor = color.cgColor
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 10, dy: 0 )
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 10, dy: 0 )
}
}
class SecureTextField: TextField {
var actualText: String = ""
override var text: String! {
get {
if isEditing {
return super.text
} else {
return actualText
}
}
set {
super.text=(newValue)
}
}
override func setup() {
super.setup()
clearsOnBeginEditing = true
}
override func setupEvents () {
super.setupEvents()
addTarget(self, action: #selector(SecureTextField.editingDidChange(_:)), for: .editingChanged)
}
override func editingDidBegin (_ sender: AnyObject) {
super.editingDidBegin(sender)
isSecureTextEntry = true
actualText = text
}
func editingDidChange(_ sender: AnyObject) {
actualText = text
}
override func editingDidFinish(_ sender: AnyObject) {
super.editingDidFinish(sender)
isSecureTextEntry = false
actualText = text
text = dotPlaceholder()
}
func dotPlaceholder() -> String {
var index = 0
let dots = NSMutableString()
while (index < text.count) {
dots.append("•")
index += 1
}
return dots as String
}
}